package com.example.cash.craft.controller;

import com.example.cash.craft.pojo.param.CategoryAddNewParam;
import com.example.cash.craft.pojo.param.CategoryUpdateParam;
import com.example.cash.craft.pojo.vo.CategoryVO;
import com.example.cash.craft.pojo.vo.PageData;
import com.example.cash.craft.service.ICategoryService;
import com.example.cash.craft.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "3. 类别管理模块")
@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @Operation(summary = "[管理] 新增类别")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@ParameterObject @Valid CategoryAddNewParam param) {
        categoryService.addNew(param);
        return JsonResult.ok();
    }

    @Operation(summary = "[管理] 删除类别")
    @ApiOperationSupport(order = 200)
    @Parameter(name = "id", description = "类别ID", required = true, example = "1")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @Operation(summary = "[管理] 修改类别")
    @ApiOperationSupport(order = 300)
    @Parameter(name = "id", description = "类别ID", required = true, example = "1")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/{id}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, @ParameterObject @Valid CategoryUpdateParam param) {
        categoryService.updateById(id, param);
        return JsonResult.ok();
    }

    @Operation(summary = "[游客] 根据ID查询类别")
    @ApiOperationSupport(order = 400)
    @Parameter(name = "id", description = "类别ID", required = true, example = "1")
    @GetMapping("/{id}")
    public JsonResult<CategoryVO> selectById(@PathVariable Long id) {
        CategoryVO data = categoryService.selectById(id);
        return JsonResult.ok(data);
    }

    @Operation(summary = "[游客] 查询类别列表")
    @ApiOperationSupport(order = 410)
    @Parameter(name = "page", description = "页码", example = "1", required = false)
    @GetMapping()
    public JsonResult<PageData<CategoryVO>> listAll(Integer page) {
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<CategoryVO> data = categoryService.listAll(page);
        return JsonResult.ok(data);
    }

    @Operation(summary = "[管理] 重建缓存")
    @ApiOperationSupport(order = 500)
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/rebuild-cache")
    public JsonResult<Void> rebuildCache() {
        categoryService.rebuildCache();
        return JsonResult.ok();
    }

}